package com.genersoft.iot.vmp.utils;

import com.genersoft.iot.vmp.conf.UserSetting;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ：mengfanlu
 * @date ：Created in 2022/2/14 9:34
 * @description：文件工具类
 * @modified By：
 * @version: ${1.0.0}
 */
@Component
public class FileUtils {

    @Autowired
    private UserSetting userSetting;


    public String fileWrite(byte[] data) {

        long timeSuffix = System.currentTimeMillis();
        String fileName = "audio_" + timeSuffix + ".mp3";

        Date date = new Date(timeSuffix);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateDir = simpleDateFormat.format(date);

        File file = new File(userSetting.getMp3Path(), dateDir);
        if (!file.exists()) {
            file.mkdirs();
        }

        String finalPath = file.getAbsolutePath() + File.separator + fileName;

        FileOutputStream outputStream = null;

        try {
            outputStream = new FileOutputStream(finalPath);
            //IOUtils.write(data,outputStream);
            IOUtils.write(data,outputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }

        return finalPath;
    }

    public String fileWrite(String path, String fileName, InputStream inputStream) {
        byte[] cache = new byte[2048];
        long time = System.currentTimeMillis();

        path += time + "_" + fileName;
        int len;
        try {
            FileOutputStream outputStream = new FileOutputStream(path);
            while ((len = inputStream.read(cache)) != -1) {
                outputStream.write(cache, 0, len);
            }
            if (outputStream != null) {
                outputStream.close();
            }
            if (inputStream != null) {
                outputStream.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return path;
    }

    public String mp3ToPcm(String mp3FilePath) {

        long timeSuffix = System.currentTimeMillis();
        Date date = new Date(timeSuffix);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        String dateDir = simpleDateFormat.format(date);
        File file = new File(userSetting.getPcmaPath(), dateDir);

        if (!file.exists()) {
            file.mkdirs();
        }


        String pcmFileName = timeSuffix + "_audio" + ".pcm";
        String pcmFileFinalPath = file.getAbsolutePath() + File.separator + pcmFileName;

        try {

            String command1 = "ffmpeg  -i ";
            String command2 = " -acodec pcm_alaw -f alaw -ac 1 -ar 8000 ";

            Runtime runtime = Runtime.getRuntime();
            String finalCommand = command1 + mp3FilePath + command2 + pcmFileFinalPath;
            Process exec = runtime.exec(finalCommand);
            exec.waitFor();

            exec.destroy();

        } catch (Exception e) {
            System.out.println("MP3转换PCM文件 失败");
        }

        return pcmFileFinalPath;
    }
}
